﻿/*
 * @author: wizardc
 */

using Dou.UI;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;

namespace DouEditor.UI
{
    [CustomEditor(typeof(DInputField), true)]
    public class DInputFieldEditor : InputFieldEditor
    {
        [MenuItem("GameObject/DouUI/InputField - Legacy", false, 10)]
        private static void CreateUI(MenuCommand menuCommand)
        {
            var go = UIEditorUtil.CreateUIElement("InputField (Legacy)");
            
            var childPlaceholder = UIEditorUtil.CreateGameObject("Placeholder", go.transform);
            var childText = UIEditorUtil.CreateGameObject("Text", go.transform);

            var image = go.AddComponent<DImage>();
            image.type = Image.Type.Sliced;

            var inputField = go.AddComponent<DInputField>();
            var colors = inputField.colors;
            colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
            colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
            colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);

            var text = childText.AddComponent<DText>();
            text.text = "";
            text.supportRichText = false;
            text.color = Color.black;

            var placeholder = childPlaceholder.AddComponent<DText>();
            placeholder.text = "Enter text...";
            placeholder.fontStyle = FontStyle.Italic;
            Color placeholderColor = text.color;
            placeholderColor.a *= 0.5f;
            placeholder.color = placeholderColor;

            RectTransform textRectTransform = childText.GetComponent<RectTransform>();
            textRectTransform.anchorMin = Vector2.zero;
            textRectTransform.anchorMax = Vector2.one;
            textRectTransform.sizeDelta = Vector2.zero;
            textRectTransform.offsetMin = new Vector2(10, 6);
            textRectTransform.offsetMax = new Vector2(-10, -7);

            RectTransform placeholderRectTransform = childPlaceholder.GetComponent<RectTransform>();
            placeholderRectTransform.anchorMin = Vector2.zero;
            placeholderRectTransform.anchorMax = Vector2.one;
            placeholderRectTransform.sizeDelta = Vector2.zero;
            placeholderRectTransform.offsetMin = new Vector2(10, 6);
            placeholderRectTransform.offsetMax = new Vector2(-10, -7);

            inputField.textComponent = text;
            inputField.placeholder = placeholder;
            
            UIEditorUtil.PlaceUIElementRoot(go, menuCommand);
        }
        
        private SerializedProperty _textComponent;
        
        private SerializedProperty _onInputChanged;
        private SerializedProperty _onInputStart;
        private SerializedProperty _onInputEnd;
        private SerializedProperty _onKeyboardOpening;
        private SerializedProperty _onKeyboardClosed;

        protected override void OnEnable()
        {
            base.OnEnable();
            _textComponent = serializedObject.FindProperty("m_TextComponent");
            _onInputChanged = serializedObject.FindProperty("OnInputChanged");
            _onInputStart = serializedObject.FindProperty("OnInputStart");
            _onInputEnd = serializedObject.FindProperty("OnInputEnd");
            _onKeyboardOpening = serializedObject.FindProperty("OnKeyboardOpening");
            _onKeyboardClosed = serializedObject.FindProperty("OnKeyboardClosed");
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();
            using (new EditorGUI.DisabledScope(_textComponent == null || _textComponent.objectReferenceValue == null))
            {
                EditorGUILayout.PropertyField(_onInputChanged);
                EditorGUILayout.PropertyField(_onInputStart);
                EditorGUILayout.PropertyField(_onInputEnd);
                EditorGUILayout.PropertyField(_onKeyboardOpening);
                EditorGUILayout.PropertyField(_onKeyboardClosed);
            }
            serializedObject.ApplyModifiedProperties();
        }
    }
}
